import Router from 'koa-router'
import httpErrors from '../../../../core/HttpException'
import Mysql from '../../../../core/Mysql'
import Auth from '../../../../middlewares/Auth'
import { getTreeByList } from '../../../../common/utils/utils'
import Config from '../../../../config/Config'

const router = new Router({
  prefix: `${Config.api_prefix}lowCode/boardCategoryManager`,
})

/*
 * 获取分类树形结构
 * @return
 */
router.get('/getBoardCategoryTree', Auth.verifyToken, async () => {
  const rootId = 0
  const name = `getBoardCategoryChildList(${rootId})`
  const res = await Mysql.command(`
    SELECT
      *
    FROM
      board_category
    WHERE
      FIND_IN_SET(
        id,
        ${name}
      )
  `)
  // const list = res.results[0][name].split(',')
  throw new httpErrors.Success(getTreeByList(res.results, 0))
})

export default router
